#ifndef COMMANDEXECUTOR_H
#define COMMANDEXECUTOR_H

#include <QObject>
#include <QDesktopServices>
#include <QUrl>
#include <QProcess>
#include <QFileInfo>
#include <QDir>
#include <QDebug>
#include <windows.h>
#include <shellapi.h>
#include <QCoreApplication>

#include <QString>
#include <QUrl>
#include <shlobj.h>
#include <objbase.h>

#include <QRegularExpression>
#include <QThread>
#include <QMessageBox>


//快捷项类型
enum TShortCutType {
    scOther, scItem, scBlank, scRemark
};
//参数类型
enum TParamType {
    ptNone, ptNoEncoding, ptURLQuery, ptUTF8Query
};

class TShortCutItem {
public:
    TShortCutType ShortCutType;
    TParamType ParamType;
    QString ShortCut;
    QString Name;
    QString CommandLine;
    int Rank;
    int Freq;
};

class TCmdObject {
public:
    QString Command;
    QString WorkingDir;
    QString Param;
    TParamType ParamType;
};

class CommandExecutor : public QObject
{
    Q_OBJECT
public:
    explicit CommandExecutor(QObject *parent = nullptr);
    // 定义系统级标识符和对应的GUID
    QMap<QString, QString> systemIdentifiers = {
        {"我的电脑", "::{20D04FE0-3AEA-1069-A2D8-08002B30309D}"},
        {"我的文档", "::{450D8FBA-AD25-11D0-98A8-0800361B1103}"},
        {"控制面板", "::{21EC2020-3AEA-1069-A2DD-08002B30309D}"},
        {"打印机", "::{2227A280-3AEA-1069-A2DE-08002B30309D}"},
        {"记事本", "::{1FBA04EE-3024-11D2-8F1F-0000F87ABD16}"},
        {"网络邻居", "::{208D2C60-3AEA-1069-A2D7-08002B30309D}"},
        {"回收站", "::{645FF040-5081-101B-9F08-00AA002F954E}"},
        {"字体", "::{BD84B380-8CA2-1069-AB1D-08000948F534}"}
    };

    //https://blog.csdn.net/bruce_wang_janet/article/details/8592792
    void executeCommand(const QString &command);

    //https://blog.csdn.net/qq_38446366/article/details/79714830
    void executeCommand(const QString &command,QStringList &arguments);

    bool ExtractShortCutItemFromFileName(TShortCutItem &ShortCutItem, const QString &FileName);

    QString ResolveLink(const QString &ALinkFile);

    QString ExtractFileName(const QString &FileName);

    QString ExtractFileExt(const QString &FileName);

    QString RemoveQuotationMark(const QString &str, const QString &mark);


    QString RemoveQuotationMark2(const QString &str, const QString &QuotationMark);


    bool ReplaceEnvStr(QString &str);


    QString ExtractFileDir(const QString &FileName);

    bool Execute(TCmdObject *cmdobj);
    bool Execute(TShortCutItem *ShortCutItem);
private:
    // 创建一个 QProcess 实例
    QProcess process;
    QRegularExpression m_Regex;
    int ShortCutRunCount = 0;
    void openUrl(const QString &url) {
        QDesktopServices::openUrl(QUrl(url));
    }

    void startExecutable(const QString &path,QStringList &arguments) {
        QProcess::startDetached(path, arguments);
    }

    void openShortcut(const QString &path) {
        QProcess::startDetached("explorer.exe", QStringList() << path);
    }

    void startProcess(const QString &command,QStringList &arguments) {
        QProcess::startDetached(command,arguments);
    }

    void openDrive(const QString &drive) {
        QString path = drive + "/";
        QProcess::startDetached("explorer.exe", QStringList() << path);
    }
    void runProgram(const QString &program) {
        // Convert QString to LPCWSTR
        LPCWSTR programW = reinterpret_cast<LPCWSTR>(program.utf16());
        HINSTANCE result = ShellExecute(NULL, L"open", programW, NULL, NULL, SW_SHOWNORMAL);
        if ((intptr_t)result <= 32) {
            qDebug() << "Failed to run program";
        }
    }
signals:
};

#endif // COMMANDEXECUTOR_H
